package us.kg.kotoriforest.springmvc.restful.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 对请求的返回结果封装
 *
 * @author Kotori rotroutine@163.com
 * @since 2025/2/8
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class R<T> {
    private Integer code;
    private String message;
    private T data;

    /**
     * 请求成功
     *
     * @param data 返回的数据
     * @param <T>  返回的数据类型
     * @return 封装的返回结果
     */
    public static <T> R<T> success(T data) {
        return new R<>(200, "success", data);
    }

    public static <T> R<T> success() {
        return new R<>(200, "success", null);
    }

    public static <T> R<T> error() {
        return new R<>(500, "error", null);
    }

    public static <T> R<T> error(T data) {
        return new R<>(500, "error", data);
    }

    public static <T> R<T> error(Integer code, String message, T data) {
        return new R<>(code, message, data);
    }

    public static <T> R<T> notFound() {
        return new R<>(404, "not found", null);
    }
}
